package com.study.leetcode.summary;

public class BinarySearch {
    public int binarySearch(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        int middle = 0;
        if (nums.length < 1 || target < nums[start] || target > nums[end]) {
            return -1;
        }
        while (start <= end) {
            middle = (start + end) / 2;
            if (nums[middle] == target) {
                return middle;
            } else if (nums[middle] < target) {
                start = middle + 1;
            } else {
                end = middle - 1;
            }
        }
        return -1;
    }
}
